//package com.lengyu.kangdeli.security.rest;
//
//import com.fasterxml.jackson.annotation.JsonProperty;
//import com.lengyu.kangdeli.security.jwt.JWTFilter;
//import com.lengyu.kangdeli.security.jwt.TokenProvider;
//import com.lengyu.kangdeli.security.rest.dto.LoginDto;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//
///**
// * Controller to authenticate users.
// */
//@RestController
//@RequestMapping("/api")
//public class AuthenticationRestController {
//
//   private final TokenProvider tokenProvider;
//
//   private final AuthenticationManagerBuilder authenticationManagerBuilder;
//
//   public AuthenticationRestController(TokenProvider tokenProvider, AuthenticationManagerBuilder authenticationManagerBuilder) {
//      this.tokenProvider = tokenProvider;
//      this.authenticationManagerBuilder = authenticationManagerBuilder;
//   }
//
//   /**
//    * 通过这个方法越过了usernamepassword的filter
//    * @param loginDto
//    * @return
//    */
//   @PostMapping("/authenticate")
//   public ResponseEntity<JWTToken> authorize( @RequestBody LoginDto loginDto) {
//
//      UsernamePasswordAuthenticationToken authenticationToken =
//         new UsernamePasswordAuthenticationToken(loginDto.getUsername(), loginDto.getPassword());
//
//      Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
//      SecurityContextHolder.getContext().setAuthentication(authentication);
//
//      boolean rememberMe = (loginDto.getRememberMe() == null) ? false : loginDto.getRememberMe();
//      String jwt = tokenProvider.createToken(authentication, rememberMe);
//
//      HttpHeaders httpHeaders = new HttpHeaders();
//      httpHeaders.add(JWTFilter.AUTHORIZATION_HEADER, "Bearer " + jwt);
//
//      return new ResponseEntity<>(new JWTToken(jwt), httpHeaders, HttpStatus.OK);
//   }
//
//   /**
//    * Object to return as body in JWT Authentication.
//    */
//   static class JWTToken {
//
//      private String idToken;
//
//      JWTToken(String idToken) {
//         this.idToken = idToken;
//      }
//
//      @JsonProperty("id_token")
//      String getIdToken() {
//         return idToken;
//      }
//
//      void setIdToken(String idToken) {
//         this.idToken = idToken;
//      }
//   }
//}
